package cn.pugle.oj.leetcode;// 59
// https://leetcode.com/problems/spiral-matrix-ii/
// ?有没有可能有数学做法?

import cn.pugle.oj.catalog.Unknown;

class LC59  implements Unknown {
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        recFill(result, 0, 0, n, 1);
        return result;
    }

    public void recFill(int[][] result, int i, int j, int l, int startNum) {
        if (l == 0) return;
        if (l == 1) {result[i][j] = startNum; return;}
        //上
        for(int k=0; k<l-1; k++) {
            result[i][j + k] = startNum;
            startNum++;
        }
        //右
        for(int k=0; k<l-1; k++) {
            result[i + k][j + l - 1] = startNum;
            startNum++;
        }
        //下
        for(int k=0; k<l-1; k++) {
            result[i + l - 1][j + l - 1 - k] = startNum;
            startNum++;
        }
        //左
        for(int k=0; k<l-1; k++) {
            result[i + l - 1 - k][j] = startNum;
            startNum++;
        }
        System.out.println(result);
        recFill(result, i+1, j+1, l-2, startNum);
        return;
    }
}